﻿
using System;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

namespace  BOMS.Utility
{

    public class EnumUtility<T> where T : struct, IComparable, IFormattable, IConvertible
        {
            public EnumUtility()
            {
                 if (typeof(Enum) != typeof(T).BaseType)
                 {
                     throw new Exception("类型参数不合法，当前泛型类的类型参数必须为枚举类型");
                 }
            }       
            
            public struct EnumInfo
            {
                public T Item;
                public int Value;
                public string Descr;
            }
            public static List<EnumInfo> GetEnumList()
            {
                Type ty = typeof(T);
                List<EnumInfo> list = new List<EnumInfo>();
                foreach (string s in Enum.GetNames(ty))
                {
                    //Console.WriteLine("{0,-11}= {1}", s, Enum.Format(weekdays, Enum.Parse(weekdays, s), "d"));
                    EnumInfo ei = new EnumInfo();
                    ei.Item = (T)Enum.Parse(ty, s);
                    ei.Value = Convert.ToInt32(ei.Item);
                    Enum x = ei.Item as Enum;
                    if (x != null)
                    {
                        ei.Descr = x.ToDescription();
                    }
                    list.Add(ei);
                }
                return list;
            }
        }


        /// <summary>
        /// 扩展 Enum 类
        /// </summary>
        public static class EnumExtensions
        {
            /// <summary>
            /// 显示中文的描述，自定义 Attribute 的类型应该为 ChineseDescriptionAttribute
            /// </summary>
            /// <param name="enumeration"></param>
            /// <returns></returns>
            public static string ToCusDescription(this Enum enumeration)
            {                
                StringBuilder retlist = new StringBuilder();
                Type type = enumeration.GetType(); // 得到枚举的类型：到底是哪个枚举
                string[] arr = enumeration.ToString().Split(',');
                for (int i = 0; i < arr.Length; i++)
                {
                    MemberInfo[] memInfos = type.GetMember(arr[i].Trim()); // 得到枚举的字段，可能零个或一个或多个
                    if (memInfos != null && memInfos.Length > 0)
                    {
                        object[] customEnumDesc = memInfos[0].GetCustomAttributes(typeof(CusDescriptionAttribute), false); // 得到枚举的自定义 Attribute
                        if (customEnumDesc != null && customEnumDesc.Length > 0)
                        {
                            retlist.Append(((CusDescriptionAttribute)customEnumDesc[0]).Text + ",");
                        }
                    }
                }
                return retlist.ToString().TrimEnd(',');
            }

            /// <summary>
            /// 显示枚举的描述，自定义 Attribute 的类型应该为 System.ComponentModel.DescriptionAttribute
            /// </summary>
            /// <param name="enumeration"></param>
            /// <returns></returns>
            public static string ToDescription(this Enum enumeration)
            {
                StringBuilder retlist = new StringBuilder();
                Type type = enumeration.GetType();
                string[] arr = enumeration.ToString().Split(',');
                for (int i = 0; i < arr.Length; i++)
                {
                    MemberInfo[] memInfo = type.GetMember(arr[i].Trim());
                    if (memInfo != null && memInfo.Length > 0)
                    {
                        object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (attrs != null && attrs.Length > 0)
                        {
                            retlist.Append(((DescriptionAttribute)attrs[0]).Description+",");
                        }
                    }
                }
                return retlist.ToString().TrimEnd(',');
            }           
        }


        [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
        public class CusDescriptionAttribute : Attribute
        {
            private string _text;
            public string Text
            {
                get
                {
                    return _text;
                }
            }
            public CusDescriptionAttribute(string text)
            {
                this._text = text;
            }
        }
    /*
        public enum Btn
        {
            /// <summary>
            /// 无
            /// </summary>
            [Description("")]
            None,
            /// <summary>
            /// 提交
            /// </summary>
            [CusDescription("提交订单")]
            [Description("提交")]
            Submit = 1,
            /// <summary>
            /// 保存
            /// </summary>
            [Description("保存")]
            Save = 2,
            /// <summary>
            /// 回退
            /// </summary>
            [Description("回退")]
            Back = 4,
            /// <summary>
            /// 委托
            /// </summary>
            [Description("委托")]
            Delegate = 8
        }
     * */
}
